3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Revision\MutableRevisionRecord
;
5 use MediaWiki\Revision\RevisionStore
;
6 use MediaWiki\Revision\SlotRecord
;
7 use MediaWiki\User\UserIdentityValue
;
12 * @covers BlockLevelPass
14 class ParserMethodsTest
extends MediaWikiLangTestCase
{
16 public static function providePreSaveTransform() {
18 [ 'hello this is ~~~',
19 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
21 [ 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
22 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
28 * @dataProvider providePreSaveTransform
30 public function testPreSaveTransform( $text, $expected ) {
31 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
33 $user->setName( "127.0.0.1" );
34 $popts = ParserOptions
::newFromUser( $user );
35 $text = MediaWikiServices
::getInstance()->getParser()
36 ->preSaveTransform( $text, $title, $user, $popts );
38 $this->assertEquals( $expected, $text );
41 public static function provideStripOuterParagraph() {
42 // This mimics the most common use case (stripping paragraphs generated by the parser).
43 $message = new RawMessage( "Message text." );
51 "<p class='foo'>Text.</p>",
52 "<p class='foo'>Text.</p>",
59 "<p>Text.</p><p>More text.</p>",
60 "<p>Text.</p><p>More text.</p>",
70 * @dataProvider provideStripOuterParagraph
72 public function testStripOuterParagraph( $text, $expected ) {
73 $this->assertEquals( $expected, Parser
::stripOuterParagraph( $text ) );
77 * @expectedException MWException
78 * @expectedExceptionMessage Parser state cleared while parsing.
79 * Did you call Parser::parse recursively?
81 public function testRecursiveParse() {
82 $title = Title
::newFromText( 'foo' );
83 $parser = MediaWikiServices
::getInstance()->getParser();
84 $po = new ParserOptions
;
85 $parser->setHook( 'recursivecallparser', [ $this, 'helperParserFunc' ] );
86 $parser->parse( '<recursivecallparser>baz</recursivecallparser>', $title, $po );
89 public function helperParserFunc( $input, $args, $parser ) {
90 $title = Title
::newFromText( 'foo' );
91 $po = new ParserOptions
;
92 $parser->parse( $input, $title, $po );
96 public function testCallParserFunction() {
97 // Normal parses test passing PPNodes. Test passing an array.
98 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
99 $parser = MediaWikiServices
::getInstance()->getParser();
100 $parser->startExternalParse( $title, new ParserOptions(), Parser
::OT_HTML
);
101 $frame = $parser->getPreprocessor()->newFrame();
102 $ret = $parser->callParserFunction( $frame, '#tag',
103 [ 'pre', 'foo', 'style' => 'margin-left: 1.6em' ]
105 $ret['text'] = $parser->mStripState
->unstripBoth( $ret['text'] );
108 'text' => '<pre style="margin-left: 1.6em">foo</pre>',
109 ], $ret, 'callParserFunction works for {{#tag:pre|foo|style=margin-left: 1.6em}}' );
114 * @covers ParserOutput::getSections
116 public function testGetSections() {
117 $title = Title
::newFromText( str_replace( '::', '__', __METHOD__
) );
118 $out = MediaWikiServices
::getInstance()->getParser()
119 ->parse( "==foo==\n<h2>bar</h2>\n==baz==\n", $title, new ParserOptions() );
127 'fromtitle' => $title->getPrefixedDBkey(),
137 'fromtitle' => false,
138 'byteoffset' => null,
147 'fromtitle' => $title->getPrefixedDBkey(),
151 ], $out->getSections(), 'getSections() with proper value when <h2> is used' );
155 * @dataProvider provideNormalizeLinkUrl
157 public function testNormalizeLinkUrl( $explanation, $url, $expected ) {
158 $this->assertEquals( $expected, Parser
::normalizeLinkUrl( $url ), $explanation );
161 public static function provideNormalizeLinkUrl() {
164 'Escaping of unsafe characters',
165 'http://example.org/foo bar?param[]="value"¶m[]=valüe',
166 'http://example.org/foo%20bar?param%5B%5D=%22value%22¶m%5B%5D=val%C3%BCe',
169 'Case normalization of percent-encoded characters',
170 'http://example.org/%ab%cD%Ef%FF',
171 'http://example.org/%AB%CD%EF%FF',
174 'Unescaping of safe characters',
175 'http://example.org/%3C%66%6f%6F%3E?%3C%66%6f%6F%3E#%3C%66%6f%6F%3E',
176 'http://example.org/%3Cfoo%3E?%3Cfoo%3E#%3Cfoo%3E',
179 'Context-sensitive replacement of sometimes-safe characters',
180 'http://example.org/%23%2F%3F%26%3D%2B%3B?%23%2F%3F%26%3D%2B%3B#%23%2F%3F%26%3D%2B%3B',
181 'http://example.org/%23%2F%3F&=+;?%23/?%26%3D%2B%3B#%23/?&=+;',
184 'IPv6 links aren\'t escaped',
185 'http://[::1]/foobar',
186 'http://[::1]/foobar',
189 'non-IPv6 links aren\'t unescaped',
190 'http://%5B::1%5D/foobar',
191 'http://%5B::1%5D/foobar',
196 public function testWrapOutput() {
197 $title = Title
::newFromText( 'foo' );
198 $po = new ParserOptions();
199 $parser = MediaWikiServices
::getInstance()->getParser();
200 $parser->parse( 'Hello World', $title, $po );
201 $text = $parser->getOutput()->getText();
203 $this->assertContains( 'Hello World', $text );
204 $this->assertContains( '<div', $text );
205 $this->assertContains( 'class="mw-parser-output"', $text );
209 * @param string $name
212 private function getMockTitle( $name ) {
213 $title = $this->getMock( Title
::class );
214 $title->method( 'getPrefixedDBkey' )->willReturn( $name );
215 $title->method( 'getPrefixedText' )->willReturn( $name );
216 $title->method( 'getDBkey' )->willReturn( $name );
217 $title->method( 'getText' )->willReturn( $name );
218 $title->method( 'getNamespace' )->willReturn( 0 );
219 $title->method( 'getPageLanguage' )->willReturn( Language
::factory( 'en' ) );
224 public function provideRevisionAccess() {
225 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
227 $frank = $this->getMockBuilder( User
::class )
228 ->disableOriginalConstructor()
231 $frank->method( 'getName' )->willReturn( 'Frank' );
233 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
234 $po = new ParserOptions( $frank );
236 yield
'current' => [ $text, $po, 0, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
237 yield
'current' => [ $text, $po, null, 'user:;id:;time:' ];
238 yield
'current with ID' => [ $text, $po, 200, 'user:CurrentAuthor;id:200;time:20160606000000;' ];
240 $text = '* user:{{REVISIONUSER}};id:{{REVISIONID}};time:{{REVISIONTIMESTAMP}};';
241 $po = new ParserOptions( $frank );
243 yield
'old' => [ $text, $po, 100, 'user:OldAuthor;id:100;time:20140404000000;' ];
245 $oldRevision = new MutableRevisionRecord( $title );
246 $oldRevision->setId( 100 );
247 $oldRevision->setUser( new UserIdentityValue( 7, 'FauxAuthor', 0 ) );
248 $oldRevision->setTimestamp( '20141111111111' );
249 $oldRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( 'FAUX' ) );
251 $po = new ParserOptions( $frank );
252 $po->setCurrentRevisionCallback( function () use ( $oldRevision ) {
253 return new Revision( $oldRevision );
256 yield
'old with override' => [ $text, $po, 100, 'user:FauxAuthor;id:100;time:20141111111111;' ];
258 $text = '* user:{{REVISIONUSER}};user-subst:{{subst:REVISIONUSER}};';
260 $po = new ParserOptions( $frank );
261 $po->setIsPreview( true );
263 yield
'preview without override, using context' => [
271 $text = '* user:{{REVISIONUSER}};time:{{REVISIONTIMESTAMP}};'
272 . 'user-subst:{{subst:REVISIONUSER}};time-subst:{{subst:REVISIONTIMESTAMP}};';
274 $newRevision = new MutableRevisionRecord( $title );
275 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
276 $newRevision->setTimestamp( '20180808000000' );
277 $newRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( 'NEW' ) );
279 $po = new ParserOptions( $frank );
280 $po->setIsPreview( true );
281 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
282 return new Revision( $newRevision );
289 'user:NewAuthor;time:20180808000000;',
290 'user-subst:NewAuthor;time-subst:20180808000000;',
293 $po = new ParserOptions( $frank );
294 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
295 return new Revision( $newRevision );
298 yield
'pre-save' => [
302 'user:NewAuthor;time:20180808000000;',
303 'user-subst:NewAuthor;time-subst:20180808000000;',
306 $text = "(ONE)<includeonly>(TWO)</includeonly>"
307 . "<noinclude>#{{:ParserRevisionAccessTest}}#</noinclude>";
309 $newRevision = new MutableRevisionRecord( $title );
310 $newRevision->setUser( new UserIdentityValue( 9, 'NewAuthor', 0 ) );
311 $newRevision->setTimestamp( '20180808000000' );
312 $newRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
314 $po = new ParserOptions( $frank );
315 $po->setIsPreview( true );
316 $po->setCurrentRevisionCallback( function () use ( $newRevision ) {
317 return new Revision( $newRevision );
320 yield
'preview with self-transclude' => [ $text, $po, null, '(ONE)#(ONE)(TWO)#' ];
324 * @dataProvider provideRevisionAccess
326 public function testRevisionAccess(
331 $expectedInPst = null
333 $title = $this->getMockTitle( 'ParserRevisionAccessTest' );
335 $po->enableLimitReport( false );
337 $oldRevision = new MutableRevisionRecord( $title );
338 $oldRevision->setId( 100 );
339 $oldRevision->setUser( new UserIdentityValue( 7, 'OldAuthor', 0 ) );
340 $oldRevision->setTimestamp( '20140404000000' );
341 $oldRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( 'OLD' ) );
343 $currentRevision = new MutableRevisionRecord( $title );
344 $currentRevision->setId( 200 );
345 $currentRevision->setUser( new UserIdentityValue( 9, 'CurrentAuthor', 0 ) );
346 $currentRevision->setTimestamp( '20160606000000' );
347 $currentRevision->setContent( SlotRecord
::MAIN
, new WikitextContent( 'CURRENT' ) );
349 $revisionStore = $this->getMockBuilder( RevisionStore
::class )
350 ->disableOriginalConstructor()
354 ->method( 'getKnownCurrentRevision' )
356 [ $title, 100, $oldRevision ],
357 [ $title, 200, $currentRevision ],
358 [ $title, 0, $currentRevision ],
362 ->method( 'getRevisionById' )
364 [ 100, 0, $oldRevision ],
365 [ 200, 0, $currentRevision ],
368 $this->setService( 'RevisionStore', $revisionStore );
370 $parser = MediaWikiServices
::getInstance()->getParser();
371 $parser->parse( $text, $title, $po, true, true, $revId );
372 $html = $parser->getOutput()->getText();
374 $this->assertContains( $expectedInHtml, $html, 'In HTML' );
376 if ( $expectedInPst !== null ) {
377 $pst = $parser->preSaveTransform( $text, $title, $po->getUser(), $po );
378 $this->assertContains( $expectedInPst, $pst, 'After Pre-Safe Transform' );
382 public static function provideGuessSectionNameFromWikiText() {
384 [ '1/2', 'html5', '#1/2' ],
385 [ '1/2', 'legacy', '#1.2F2' ],
389 /** @dataProvider provideGuessSectionNameFromWikiText */
390 public function testGuessSectionNameFromWikiText( $input, $mode, $expected ) {
391 $this->setMwGlobals( [ 'wgFragmentMode' => [ $mode ] ] );
392 $result = MediaWikiServices
::getInstance()->getParser()
393 ->guessSectionNameFromWikiText( $input );
394 $this->assertEquals( $result, $expected );
397 // @todo Add tests for cleanSig() / cleanSigInSig(), getSection(),
398 // replaceSection(), getPreloadText()